package com.lss.cloud.mygateway;

import lombok.Getter;
import lombok.Setter;
import org.bouncycastle.pqc.crypto.picnic.PicnicPublicKeyParameters;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * 2024/3/18-23:11
 * 功能:
 */
@Component
public class MyGatewayFilterFactory extends AbstractGatewayFilterFactory<MyGatewayFilterFactory.Config> {

    public MyGatewayFilterFactory(){
        super(Config.class);
    }

    public static class Config{
        @Getter@Setter
        private String status;
    }



    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                ServerHttpRequest request = exchange.getRequest();
                System.out.println("status："+config.getStatus());
                if (request.getQueryParams().containsKey("atguigu")){
                    return chain.filter(exchange);
                    //代表放行filter
                }else {
                    boolean b = exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
                    return exchange.getResponse().setComplete();
                }
                //return null;
            }
        };
    }

    /**
     * 用来实现快捷配置
     * @return
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("status");
    }
}
